<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色授权</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>

<!--树形组件-->
<div id="test1"></div>

<a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" onclick="grant()">授权</a>

<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>

    var roleId;
    var tree;
    var $;

    function child(roleId){
        this.roleId = roleId;
        console.log(roleId);
        layui.use(['form','element','tree'], function () {
            var form = layui.form,
                layer = layui.layer,
                element = layui.element;
                $ = layui.$;
                tree = layui.tree;

            //有层级地查询所有菜单(树形结构)
            $.get("/wfx_manager/module/queryTreeNodeListHierarchy",{
                roleId:roleId
            },function (res) {
                if (res.flag){
                    var data =res.data;
                    //渲染
                    var inst1 = tree.render({
                        elem: '#test1'  //绑定元素
                        ,data: data,
                        showCheckbox:true
                        ,id: 'demoId' //定义索引
                    });
                }
            });

        });
    }


    /**
     * 添加授权
     */
    function grant() {

        //存储选中的菜单编号
        var moduleIds = [];
        //获取选中的菜单编号
        var checkData = tree.getChecked('demoId');
        //一级菜单
        $.each(checkData,function (index,item) {
            //获取一级菜单编号
            moduleIds.push(item.id);
            var secondData=item.children;
            $.each(secondData,function (index,item) {
                //获取二级菜单编号
                moduleIds.push(item.id);
                var thirdData=item.children;
                $.each(thirdData,function (index,item) {
                    //获取三级菜单编号
                    moduleIds.push(item.id);
                });
            })
        })
        console.log(moduleIds);

        //发起授权请求
        $.post('/wfx_manager/module/grant',{
            roleId:roleId,
            moduleIds:moduleIds
        },function (res) {
            //获取iframe的唯一标识
            var iframeIndex = parent.layer.getFrameIndex(window.name);
            //关闭iframe窗口
            parent.layer.close(iframeIndex);
        });
    }



</script>
</body>
</html>